package com.appleframework.module.usermgr.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.appleframework.core.BaseContoller;
import com.appleframework.core.Page;
import com.appleframework.core.ServiceException;
import com.appleframework.module.usermgr.service.ItemMgrService;
import com.appleframework.module.usermgr.vo.Item;

import com.appleframework.module.usermgr.vo.SUser;

@Controller
@RequestMapping(value = "itemmgr")
public class ItemMgr extends BaseContoller {

	private static final Logger log = Logger.getLogger(ItemMgr.class);
	@Autowired
	private ItemMgrService itemMgrService;

	// 显示
	@RequestMapping(value = "add.htm", method = RequestMethod.POST)
	
	public String add(HttpSession Session, Item item, ModelMap model) {
		try {
			SUser user = (SUser) Session.getAttribute(USER_SESSION_KEY);
			
			item.setCreateuserid(user.getId());

			itemMgrService.addItem(item);
			List<Item> litems = itemMgrService.getItems();
			
			model.addAttribute("litems", litems);

			return "item_list";

		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "error";

		}

	}

	// 删除
	@RequestMapping(value = "delete/{id}.htm")
	public String delete(@PathVariable("id") Integer id, ModelMap model) {
		try {
			// SUser user=(SUser) Session.getAttribute(USER_SESSION_KEY);
			System.out.println("id===" + id);

			itemMgrService.deleteItemById(id);
			List<Item> litems = itemMgrService.getItems();
			model.addAttribute("litems", litems);
			return "item_list";

		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "error";

		}
	}

	// 修改
	@RequestMapping(value = "update.htm")
	public String update(HttpSession Session, Item item, ModelMap model) {
		try {
			SUser user = (SUser) Session.getAttribute(USER_SESSION_KEY);
			// new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())
			item.setUpdatetime(new Date());

			System.out.println("id===" + item.getId());
			itemMgrService.updateItem(item);
			List<Item> litems = itemMgrService.getItems();
			model.addAttribute("litems", litems);
			return "item_list";

		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "error";

		}
	}

	// 列表 ,
	@RequestMapping(value = "itemlist.htm")
	public String listItem(HttpSession Session, String nextPage,String condition, ModelMap model) {
		SUser user = (SUser) Session.getAttribute(USER_SESSION_KEY);
		Page<Item> pager = new Page<Item>();
		pager.setPageSize(10);
		if (nextPage == null || nextPage.equals("")) {
			pager.setPageNo(1);
		} else {
			pager.setPageNo(Integer.parseInt(nextPage));
		}
		StringBuffer sb = new StringBuffer();
		sb.append("select * from t_item");
		if(null != condition && !"".equals(condition)){
			sb.append(" where name like "); 
			sb.append("%");
			sb.append(condition);
			sb.append("%");
		}

		//pager = itemMgrService.getPage(pageNow, pageSize, object);
		 model.addAttribute("litems", pager);
		return "itemList";
	}

}
